package com.zhao.savepassword;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/**
 * Created by zhao on 16-2-3.
 * 设置密码
 */
public class SignInFragment extends Fragment {

    private SharedPreferences mSharedPreferences;
    private EditText mEt_Password;
    //确认密码 的输入框
    private EditText mEt_CPassword;
    private Button mBt_setPassword;
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        init();
        return view;
    }

    /**
     * 初始化
     */
    private void init(){
        mSharedPreferences = getContext().getSharedPreferences(MainActivity.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        view = View.inflate(getActivity(), R.layout.fragment_signin, null);

        mEt_Password = (EditText)view.findViewById(R.id.et_user_password);
        mEt_CPassword = (EditText)view.findViewById(R.id.et_confirm_user_password);
        mBt_setPassword = (Button)view.findViewById(R.id.bt_sign_in);

        //点击sign in 按钮 注册 密码长度大于6 且两次输入正确
        mBt_setPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = mEt_Password.getText().toString();
                String cPassword = mEt_CPassword.getText().toString();

                if (password.length() < 6) {
                    Toast.makeText(getContext(), "密码长度不得小于6", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!password.equals(cPassword)) {
                    Toast.makeText(getContext(), "两次输入密码不匹配", Toast.LENGTH_SHORT).show();
                    return;
                }

                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putString(MainActivity.KEY_PASSWORD, password);
                editor.commit();
                Toast.makeText(getContext(), "设置密码成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(),MainActivity.class);
                startActivity(intent);

            }
        });
    }
}
